package com.hss01248.dialog;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.AbilityContext;
import ohos.app.Context;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by hss on 2018/3/24.
 */

public class DialogsMaintainer {

    private static HashMap<AbilityContext, Set<CommonDialog>> dialogsOfActivity = new HashMap<>();
    private static HashMap<AbilityContext, Set<CommonDialog>> loadingDialogs = new HashMap<>();


    public static void addLoadingDialog(Context context, CommonDialog dialog) {

        if (!((context instanceof AbilitySlice || context instanceof Ability))) {
            return;
        }
        AbilityContext activity = (AbilityContext) context;

        Set<CommonDialog> dialogs = null;
        if (loadingDialogs.containsKey(activity)) {
            dialogs = loadingDialogs.get(activity);
        }
        if (dialogs == null) {
            dialogs = new HashSet<>();
            loadingDialogs.put(activity, dialogs);
        }
        dialogs.add(dialog);


    }

    public static void dismissLoading(Ability activity) {

        if (activity == null) {
            return;
        }
        if (!loadingDialogs.containsKey(activity)) {
            return;
        }
        Set<CommonDialog> dialogSet = loadingDialogs.get(activity);
        for (CommonDialog dialog : dialogSet) {
            dialog.destroy();
            //在callback内部自动会去移除在dialogsOfActivity的引用
        }
        loadingDialogs.remove(activity);

    }

    public static void dismissLoading(CommonDialog dialog) {
        Iterator<Map.Entry<AbilityContext, Set<CommonDialog>>> entryIterator = loadingDialogs.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<AbilityContext, Set<CommonDialog>> entry = entryIterator.next();
            Set<CommonDialog> dialogInterfaces = entry.getValue();
            if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
                entryIterator.remove();
                return;
            }
            Iterator<CommonDialog> iterator = dialogInterfaces.iterator();
            while (iterator.hasNext()) {
                CommonDialog dialog0 = iterator.next();
                if (dialog.equals(dialog0)) {
                    iterator.remove();
                }
            }
            if (dialogInterfaces.isEmpty()) {
                entryIterator.remove();
                return;
            }
        }
    }

    public static void addWhenShow(Context context, CommonDialog dialog) {

        if (!(context instanceof AbilitySlice || context instanceof Ability)) {
            return;
        }

        AbilityContext activity = (AbilityContext) context;

        Set<CommonDialog> dialogs = null;
        if (dialogsOfActivity.containsKey(activity)) {
            dialogs = dialogsOfActivity.get(activity);
        }
        if (dialogs == null) {
            dialogs = new HashSet<>();
            dialogsOfActivity.put(activity, dialogs);
        }
        dialogs.add(dialog);

    }

    public static void removeWhenDismiss(CommonDialog dialog) {

        try {
            Iterator<Map.Entry<AbilityContext, Set<CommonDialog>>> entryIterator = dialogsOfActivity.entrySet().iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<AbilityContext, Set<CommonDialog>> entry = entryIterator.next();
                Set<CommonDialog> dialogInterfaces = entry.getValue();
                if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
                    entryIterator.remove();
                    return;
                }
                Iterator<CommonDialog> iterator = dialogInterfaces.iterator();
                while (iterator.hasNext()) {
                    CommonDialog dialog0 = iterator.next();
                    if (dialog.equals(dialog0)) {
                        iterator.remove();
                    }
                }
                if (dialogInterfaces.isEmpty()) {
                    entryIterator.remove();
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 在这里移除已经dismiss的dialog引用
     *
     * @param activity
     */
    public static void onPause(AbilityContext activity) {
        if (!dialogsOfActivity.containsKey(activity)) {
            return;
        }
        Set<CommonDialog> dialogInterfaces = dialogsOfActivity.get(activity);
        if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
            dialogInterfaces.remove(activity);
            return;
        }
        Iterator<CommonDialog> iterator = dialogInterfaces.iterator();
        while (iterator.hasNext()) {
            CommonDialog dialog = iterator.next();
            if (!dialog.isShowing()) {
                iterator.remove();
            }
        }
        if (dialogInterfaces.isEmpty()) {
            dialogInterfaces.remove(activity);
            return;
        }

    }

    public static void onDestory(AbilityContext activity) {

        if (!dialogsOfActivity.containsKey(activity)) {
            return;
        }
        Set<CommonDialog> dialogInterfaces = dialogsOfActivity.get(activity);
        if (dialogInterfaces == null || dialogInterfaces.isEmpty()) {
            dialogInterfaces.remove(activity);
            return;
        }
        for (CommonDialog dialog : dialogInterfaces) {
            if (dialog != null && dialog.isShowing()) {
                dialog.destroy();
            }
        }
        dialogInterfaces.remove(activity);

    }
}
